/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2015年3月7日         luwei
 */

package com.coocaa.app.gamecenter.pages.home;

import android.content.Context;
import android.view.View;
import android.view.View.OnFocusChangeListener;

import com.coocaa.launcher.sidebar.setting.layout.wallpaper.OnKeyTurnPageListener;
import com.coocaa.x.uipackage.widget.SlideFocusView;
import com.tianci.appstore.ui.data.SkyAppBaseData;
import com.tianci.appstore.ui.data.SkyCategoryData;
import com.tianci.appstore.ui.widget.SkyBaseAdapter;
import com.tianci.appstore.utils.SkyConstants;

import java.util.ArrayList;
import java.util.List;

public class CategoryAdapter extends SkyBaseAdapter {

    private Context mContext = null;
    private List<SkyCategoryData> mDatas = null;
    private List<CategoryItem> mCategoryItems = new ArrayList<CategoryItem>();
    private OnKeyTurnPageListener mKeyTurnPageListener = null;
    private SlideFocusView.FocusChangedEvent mFocusChangedEvent = null;
    private SlideFocusView.FocusViewRevision mRevisions = new SlideFocusView.FocusViewRevision(
            0, SkyConstants.Div(-113),0,SkyConstants.Div(10));

    public CategoryAdapter(Context context) {
        this.mContext = context;
    }

    public void setmFocusChangedEvent(SlideFocusView.FocusChangedEvent focusChangedEvent) {
        this.mFocusChangedEvent = focusChangedEvent;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return mDatas == null ? 0 : mDatas.size();
    }

    @Override
    public int getPosition() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void refreshUI(List<SkyAppBaseData> datas) {
        // TODO Auto-generated method stub

    }

    public void updateUI(List<SkyCategoryData> datas) {
        this.mDatas = datas;
        mCategoryItems.clear();
        notifyDataSetChanaged();
    }

    public void setKeyTurnPageListener(OnKeyTurnPageListener listener) {
        this.mKeyTurnPageListener = listener;
    }

    @Override
    public View getView(int position, View convertView, boolean[] turnpage) {
        CategoryItem item = null;
        if (convertView == null) {
            item = new CategoryItem(mContext);
            convertView = item.getView();
            if (mFocusChangedEvent != null) {
                mFocusChangedEvent.registerView(convertView, mRevisions, (OnFocusChangeListener) convertView);
            }
            item.setKeyTurnPageListener(mKeyTurnPageListener);
            item.setTurnPage(turnpage);
            convertView.setTag(item);
        } else {
            item = (CategoryItem) convertView.getTag();
        }
        item.setIndex(position);
        item.setData(mDatas.get(position));
        mCategoryItems.add(item);
        return item.getView();
    }

    @Override
    public void destory() {
        for (CategoryItem item : mCategoryItems) {
            item.destory();
        }
    }

}
